#include "data_exchange_center.h"
#include <stdio.h>
#include <string.h> 

// 链表头指针
static ListNode *data_head = NULL;

// 发布数据
int DataExchangeCenterPublish(void *data, const char *name) {
    // 检查是否已经初始化
    if (data_head == NULL) {
        ListCreate((ListHead *)&data_head);
    }

    // 检查是否已经存在相同名称的数据
    ListNode *current = data_head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            return -1; // 数据名称已存在
        }
        current = current->next;
    }

    // 添加数据
    ListPushBack((ListHead *)&data_head, data, name);
    return 0; // 成功
}

// 订阅数据
void* DataExchangeCenterSubscribe(const char *name) {
    ListNode *current = data_head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            return current->data; // 找到数据
        }
        current = current->next;
    }
    return NULL; // 数据未找到
}